﻿using System;
using System.Reflection;

namespace SharePoint.Diagnostics
{
    /// <summary>
    /// Static Trace class for SharePoint logging
    /// </summary>
    public static class Trace
    {

        /// <summary>
        /// Writes the message to the SharePoint ULS log.
        /// </summary>
        /// <param name="message">The text of the trace log message. Each trace message line has a maximum length of 800 characters</param>
        /// <param name="traceSeverity">The trace severity.</param>
        /// <param name="correlationId">A unique identifier that can be used to correlate this trace messages with other messages.</param>
        /// <param name="source">The name of the product that generated the trace log message.</param>
        /// <param name="category">The trace log category assigned to the trace log message. This should match one of the trace log categories defined for the application generating the trace log message.</param>
        public static void LogMessage(string message, TraceSeverity traceSeverity, Guid correlationId, string source, string category)
        {
            const string applicationName = "SharePoint.Diagnostics";

            TraceProvider.RegisterTraceProvider();
            TraceProvider.WriteTrace(
                traceSeverity,
                correlationId,
                applicationName,
                source,
                category,
                message);

            TraceProvider.UnregisterTraceProvider();
        }

        /// <summary>
        /// Writes the message to the SharePoint ULS log.
        /// </summary>
        /// <param name="message">The text of the trace log message. Each trace message line has a maximum length of 800 characters</param>
        /// <param name="traceSeverity">The trace severity.</param>
        /// <param name="source">The name of the product that generated the trace log message.</param>
        /// <param name="category">The trace log category assigned to the trace log message. This should match one of the trace log categories defined for the application generating the trace log message.</param>
        public static void LogMessage(string message, TraceSeverity traceSeverity, string source, string category)
        {
            Guid correlationId = new Guid();
            LogMessage(message, traceSeverity, correlationId, source, category);
        }

        /// <summary>
        /// Writes the message to the SharePoint ULS log.
        /// </summary>
        /// <param name="message">The text of the trace log message. Each trace message line has a maximum length of 800 characters</param>
        /// <param name="traceSeverity">The trace severity.</param>
        /// <param name="source">The name of the product that generated the trace log message.</param>
        public static void LogMessage(string message, TraceSeverity traceSeverity, string source)
        {
            const string category = "";
            LogMessage(message, traceSeverity, source, category);
        }

        /// <summary>
        /// Writes the message to the SharePoint ULS log.
        /// </summary>
        /// <param name="message">The text of the trace log message. Each trace message line has a maximum length of 800 characters</param>
        /// <param name="source">The name of the product that generated the trace log message.</param>
        public static void LogInformationEvent(string message, string source)
        {
            const TraceSeverity traceSeverity = TraceSeverity.InformationEvent;
            LogMessage(message, traceSeverity, source);
        }


        /// <summary>
        /// Logs the error in the SharePoint ULS log.
        /// </summary>
        /// <param name="exception">The exception to be logged.</param>
        /// <param name="source">The name of the product that generated the trace log message.</param>
        public static void LogError(Exception exception, string source)
        {
            // create error message
            string message = exception.Message + " " + exception.StackTrace;

            // add any inner exceptions
            Exception innerException = exception.InnerException;
            while (innerException != null)
            {
                message += "Inner Error: " + innerException.Message + " " + innerException.StackTrace;
                innerException = innerException.InnerException;
            }

            LogMessage(message, TraceSeverity.Exception, source, exception.GetType().ToString());
        }
    }
}
